﻿Imports System.Web
Imports System.Web.Services
Imports MeoTom.Core.BLL
Imports MeoTom.Core

Public Class Dashboard
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/html"
        If context.Request.QueryString("action") = "Top5Comments" Then
            Dim cm As New Global.MeoTom.Core.BLL.Comments()
            Dim vItem As Integer = 0
            Dim ds As New DataSet()
            Dim dbTable As New DataTable()
            Dim strOut As New StringBuilder()
            ds = cm.GetAll()
            dbTable = ds.Tables(0)
            For Each row As DataRow In dbTable.Rows
                If vItem < 5 Then
                    If vItem = 4 Then
                        strOut.Append("<div class=""ItemCommentLast"" onmouseover=""javascript:Util.DisplayPostAction('PostAction_").Append(row("CommentsID").ToString()).Append("');"" onmouseout=""javascript:Util.HidePostAction('PostAction_").Append(row("CommentsID").ToString()).Append("');"">")
                    Else
                        strOut.Append("<div class=""ItemComment"" onmouseover=""javascript:Util.DisplayPostAction('PostAction_").Append(row("CommentsID").ToString()).Append("');"" onmouseout=""javascript:Util.HidePostAction('PostAction_").Append(row("CommentsID").ToString()).Append("');"">")
                    End If
                    strOut.Append("<p class=""cmdetail"">From ").Append(row("AddedBy").ToString()).Append(" on <a href=""").Append(Left(MeoTom.Core.Configs.HomeUrl, MeoTom.Core.Configs.HomeUrl.Length - 1)).Append(row("ArticlePath").ToString()).Append(""">").Append(row("Title").ToString()).Append("</a>").Append("</p>")
                    strOut.Append("<p>").Append(row("Body").ToString()).Append("</p>")
                    strOut.Append("<div class=""PostAction"" style=""display:none;margin-top:11px;"" id=""PostAction_").Append(row("CommentsID").ToString()).Append(""">").Append(CommentAction(row("Status").ToString(), row("CommentsID").ToString(), row("ArticlesId").ToString())).Append("</div>")
                    strOut.Append("</div>")
                End If
                vItem = vItem + 1
            Next
            strOut.Append("<div class=""major-publishing-actions""><input type=""button"" name=""btnViewAll"" class=""button"" style=""background-color:#21759B;width:15%;"" value=""View all"" onclick=""javascript:location.href='ArticleComment.aspx'"" /></div>")
            dbTable = Nothing
            ds = Nothing
            cm = Nothing
            context.Response.Write(strOut.ToString())
        ElseIf context.Request.QueryString("action") = "Activity" Then
            Dim ac As New Analytics.Analytic()
            Dim strOut As New StringBuilder()
            ac.GetStatus()

            strOut.Append("<table cellpadding=""0"" cellspacing=""0"" border=""0"" width=""100%"" class=""tblDashboard"">")
            strOut.Append(" <tr>")
            strOut.Append("     <td style=""width:5%;text-align:right;""><a href=""ArticleUpdate.aspx"" class=""number"">").Append(ac.TotalArticle).Append("</a></td>")
            strOut.Append("     <td style=""width:20%;""><a href=""ArticleUpdate.aspx"">Posts</a></td>")
            strOut.Append("     <td style=""width:40%;"">&nbsp;</td>")
            strOut.Append("     <td style=""width:10%;text-align:right;""><a href=""ArticleComment.aspx"" class=""number"">").Append(ac.TotalComment).Append("</a></td>")
            strOut.Append("     <td style=""width:20%;""><a href=""ArticleComment.aspx"">Comments</a></td>")
            strOut.Append(" </tr>")
            strOut.Append(" <tr>")
            strOut.Append("     <td style=""text-align:right;""><a href=""PageManager.aspx"" class=""number"">").Append(ac.TotalPage).Append("</a></td>")
            strOut.Append("     <td><a href=""PageManager.aspx"">Pages</a></td>")
            strOut.Append("     <td>&nbsp;</td>")
            strOut.Append("     <td style=""text-align:right;""><a href=""ArticleComment.aspx?status=Approved"" class=""number"">").Append(ac.TotalComment0).Append("</a></td>")
            strOut.Append("     <td><a href=""ArticleComment.aspx?status=Approved"" class=""Approved"">Approved</a></td>")
            strOut.Append(" </tr>")
            strOut.Append(" <tr>")
            strOut.Append("     <td style=""text-align:right;""><a href=""ArticleCategory.aspx"" class=""number"">").Append(ac.TotalCat).Append("</a></td>")
            strOut.Append("     <td><a href=""ArticleCategory.aspx"">Categories</a></td>")
            strOut.Append("     <td>&nbsp;</td>")
            strOut.Append("     <td style=""text-align:right;""><a href=""ArticleComment.aspx?status=UnApproved"" class=""number"">").Append(ac.TotalComment1).Append("</a></td>")
            strOut.Append("     <td><a href=""ArticleComment.aspx?status=UnApproved"" class=""Pending"">Pending</a></td>")
            strOut.Append(" </tr>")
            strOut.Append(" <tr>")
            strOut.Append("     <td class=""last"" style=""text-align:right;""><a href=""ArticleTag.aspx"" class=""number"">").Append(ac.TotalTag).Append("</a></td>")
            strOut.Append("     <td class=""last""><a href=""ArticleTag.aspx"">Tags</a></td>")
            strOut.Append("     <td class=""last"">&nbsp;</td>")
            strOut.Append("     <td class=""last"" style=""text-align:right;"">&nbsp;</td>")
            strOut.Append("     <td class=""last"">&nbsp;</td>")
            strOut.Append(" </tr>")
            strOut.Append("</table>")

            ac = Nothing
            context.Response.Write(strOut.ToString())
        ElseIf context.Request.QueryString("action") = "GetCatName" Then
            Dim strOut As New StringBuilder
            Dim ArticleID As Integer = Integer.Parse(context.Request.QueryString("ArticleID"))
            Using dc As New MeoTomDataContext
                Dim CategorySouce = From c In dc.ArticleCategories Join tc In dc.TempArticleCats On c.CatID Equals tc.CatID Where tc.ArticleID = ArticleID Select c.CatID, c.SubCat, c.Title, c.TitleNone
                For Each CatItem In CategorySouce
                    strOut.Append("<a href=""/c").Append(CatItem.CatID).Append("s").Append(CatItem.SubCat).Append("/").Append(CatItem.TitleNone).Append(""" title=""").Append(CatItem.Title).Append(""">").Append(CatItem.Title).Append("</a>,")
                Next
            End Using
            context.Response.Write(strOut.ToString)
        ElseIf context.Request.QueryString("action") = "GetTagName" Then
            Dim objUtil As New MeoTom.Core.Utilities.Common
            Dim strOut As New StringBuilder
            Dim ArticleID As Integer = Integer.Parse(context.Request.QueryString("ArticleID"))
            Using dc As New MeoTomDataContext
                Dim TagSource = From t In dc.Tags Join ta In dc.TagArticles On t.TagID Equals ta.TagID Where ta.ArticleID = ArticleID Select t.TagID, t.TagName, t.TotalTag
                For Each TagItem In TagSource
                    strOut.Append("<a href=""/t").Append(TagItem.TagID).Append("/").Append(objUtil.utf8_to_ascii(TagItem.TagName)).Append(""" title=""").Append(TagItem.TagName).Append(""">").Append(TagItem.TagName).Append("</a>,")
                Next
            End Using
            objUtil = Nothing
            context.Response.Write(strOut.ToString)
        End If
    End Sub

    Public Function CommentAction(ByVal status As String, ByVal CommentsID As String, ByVal ArticlesId As String) As String
        Dim strOut As New StringBuilder()
        If status = "True" Then
            strOut.Append("[<a href=""ArticleComment.aspx?action=UpdateStatus&StatusID=0&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Approve"">Approve</a> | ")
        Else
            strOut.Append("[<a href=""ArticleComment.aspx?action=UpdateStatus&StatusID=1&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Unapprove"">Unapprove</a> | ")
        End If
        strOut.Append("<a onclick=""javascript:return confirm('Are you sure ?');"" style=""color:#FF0000;"" href=""ArticleComment.aspx?action=Delete&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Delete"">Delete</a> | ")
        strOut.Append("<a href=""CommentUpdate.aspx?action=Edit&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Edit"">Edit</a> | ")
        strOut.Append("<a href=""CommentUpdate.aspx?action=Reply&ArticlesId=").Append(ArticlesId).Append("&CommentsID=").Append(CommentsID).Append(""" title=""Reply"">Reply</a>]")
        Return strOut.ToString()
    End Function

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class